package com.alb.core;

import com.alb.core.netty.NettyHttpClient;
import com.alb.core.netty.NettyHttpServer;
import com.alb.core.netty.processor.NettyCoreProcessor;
import com.alb.core.netty.processor.NettyProcessor;
import lombok.extern.slf4j.Slf4j;

/**
 * 核心容器，封装netty组件
 * @author MattS
 * @date 2024/04/28
 */
@Slf4j
public class Container implements LifeCycle{

    private final Config config;

    private NettyHttpServer nettyHttpServer;

    private NettyHttpClient nettyHttpClient;

    private NettyProcessor nettyProcessor;

    public Container(Config config) {
        this.config = config;
        init();
        log.info("初始化");
    }


    @Override
    public void init() {
        //netty核心处理器
        this.nettyProcessor= new NettyCoreProcessor();
        //netty服务器
        this.nettyHttpServer= new NettyHttpServer(config, nettyProcessor);

        this.nettyHttpClient=new NettyHttpClient(config,nettyHttpServer.getEventLoopGroupWorker());
    }

    @Override
    public void start() {
        nettyHttpServer.start();
        nettyHttpClient.start();
        log.info("api-gaetway started!");
    }

    @Override
    public void shutdown() {
        nettyHttpServer.shutdown();
        nettyHttpClient.shutdown();
    }
}
